package org.gd136.ce.stage;

import java.util.ArrayList;
import java.util.List;

import org.gd136.ce.game.GS;
import org.gd136.ce.manager.RM;

import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Disposable;

public abstract class Layer extends Group implements Disposable {
	protected Image mask;
	protected List<Disposable> disposeList = new ArrayList<>();

	Layer() {
		setSize(GS.stage.getWidth(), GS.stage.getHeight());
	}

	protected void addDisposeSign(Disposable dis) {
		if (!disposeList.contains(dis)) {
			disposeList.add(dis);
		}
	}

	@Override
	public void dispose() {
		System.out.println(this.getClass().getName() + ":Layer Dispose");
		for (Disposable dis : disposeList) {
			dis.dispose();
		}
		disposeList.clear();
	}

	public static Image image(String name) {
		return new Image(RM.loadGetTexture(name));
	}

	@Override
	protected void setStage(Stage stage) {
		if (stage == null) {
			removeFromStage();
		}
		super.setStage(stage);
	}

	protected void removeFromStage() {
		dispose();
	}
}
